import random


class Task:
    def __init__(self, task_id, task_description, position, task_type='math', correct_answer=None, points=10):
        """
        :param task_id: 任务的唯一ID
        :param task_description: 任务的描述文本（比如 "2 + 3 = ?"）
        :param position: 任务触发的位置 (row, col)
        :param task_type: 任务类型，默认是 'math'
        :param correct_answer: 任务的正确答案
        :param points: 任务完成后奖励的分数
        """
        self.task_id = task_id
        self.description = task_description
        self.position = position  # (row, col) 任务触发的地点
        self.task_type = task_type
        self.correct_answer = correct_answer
        self.points = points

    def execute(self):
        """执行任务，返回任务描述"""
        return self.description

    def check_answer(self, player_answer):
        """检查玩家答案是否正确"""
        if self.task_type == 'math':
            return str(player_answer) == str(self.correct_answer)
        else:
            return False  # 默认情况下答案是错误的

    def get_reward(self):
        """返回任务完成后的奖励（分数）"""
        return self.points

    @staticmethod
    def generate_random_task(task_id, position):
        """生成一个随机任务"""
        task_type = random.choice(['math', 'maze'])  # 随机选择任务类型
        if task_type == 'math':
            num1 = random.randint(1, 10)
            num2 = random.randint(1, 10)
            correct_answer = num1 + num2
            description = f"Solve: {num1} + {num2} = ?"
            return Task(task_id, description, position, 'math', correct_answer, points=10)
        elif task_type == 'maze':
            description = "Escape the small maze!"
            return Task(task_id, description, position, 'maze', correct_answer=None, points=20)

